package com.sx.controller;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sx.bean.PtOrgan;
import com.sx.bean.PtRRoleOrgan;
import com.sx.bean.PtUser;
import com.sx.dao.PtOrganDao;
import com.sx.service.PtUserService;
import com.sx.util.Pagination;

@Controller
@RequestMapping("/user")
public class PtUserController {
	
	@Autowired
	private PtUserService ptUserService;
	
	/*
	 * 分页获取用户信息
	 */
	@RequestMapping("/getUsers")
	@ResponseBody
	public Pagination<PtUser> getUsers(int pageNumber, int pageSize){	
		Pagination<PtUser> page = ptUserService.getUsers(pageNumber, pageSize);
		return page;
	}
	
	/*
	 * 跳转到更新页面
	 */
	@RequestMapping("/updateUser")
	public String updateUser(Model model,String id){
		model.addAttribute("id", id);
		return "user/updateUser";
	}
	
	/*
	 * 更新页面发送异步请求，根据用户的uuid获取用户的详细信息
	 */
	@RequestMapping("/getUser")
	@ResponseBody
	public PtUser getUser(Model model,String uuid){
		PtUser ptUser = ptUserService.getOne(uuid);
		System.out.println(ptUser);
		return ptUser;
	}
	
	/*
	 * 根据parentId获取所有组织
	 */
	@RequestMapping("/getOrgsByParentId")
	@ResponseBody
	public List<PtOrgan> getOrgsByParentId(String parentUUID){
		List<PtOrgan> ptOrgans = ptUserService.getOrgsByParentId(parentUUID);
		return ptOrgans;
	}
	
	
	/*
	 * 根据组织uuid获取到该组织下的所有岗位信息
	 */
	@RequestMapping("/getDutyByOrganId")
	@ResponseBody
	public List<PtRRoleOrgan> getDutyByOrganId(String organId) {
		List<PtRRoleOrgan> ptRRoleOrgans = ptUserService.getDutyByOrganId(organId);
		return ptRRoleOrgans;
	}
	
	/*
	 * 更新用户信息
	 */
	@RequestMapping("/update")
	@ResponseBody
	public String update(PtUser ptUser,String[] duty) {
		ptUserService.update(ptUser,duty);
		return "1";
	}
	
	/*
	 * 删除用户
	 */
	@RequestMapping("/deleteModal")
	@ResponseBody
	public String deleteModal(String userUuid) {
		System.out.println("==================");
		ptUserService.updateStatusOfUser(userUuid);
		System.out.println("333333333333333333");
		return "success";
	}
	
	/*
	 * 新增用户信息
	 */
	@RequestMapping("/insert")
	@ResponseBody
	public String insert(PtUser ptUser,String[] duty) {
		ptUserService.insert(ptUser,duty);
		return "1";
	}
	
}
